/**
 * props 传入说明
 *      path: 权限所对应的页面路由 如果不给 则默认为当前页面路由('/test,/test/1,/test/1/2')
 *      type: (按钮类型(默认normal)-无权限的暂时-各层级权限) -> {(permissions/normal)-(disabled/hidden/tooltip)-...}
 *              (正常按钮可以不写type，非正常按钮必须给permissions-disabled...)
 *      wrapClass: 在调用页面编写样式 对应的classname
 * */
import React from 'react';
import {withRouter} from 'react-router-dom';
import {connect} from 'react-redux';
import Tools from '@untils/tools.jsx';

class PerimBtn extends React.Component {

    judgePermi(itemPermis, totalPermis) {
        let itemLength = itemPermis.length;
        let totalLength = totalPermis.length;
        let itemPermi = itemPermis[0];

        for (let k = 0; k < totalLength; k++) {
            let totalPermi = totalPermis[k];
            if (totalPermi.name === itemPermi) {
                if (itemLength > 1) {
                    let nextPremis = itemPermis.slice(1);
                    if (!!totalPermi.permissions && totalPermi.permissions.length > 0) {
                        return this.judgePermi(nextPremis, totalPermi.permissions);
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    findPermi(levels = [], permiCount = []) {
        if (!levels.length)
            return permiCount;

        let levelsItem = levels[0];
        for (let k = 0; k < permiCount.length; k++) {
            let permiItem = permiCount[k];
            if (permiItem.path === levelsItem) {
                let nextLevels = levels.slice(1);
                let nextPermiCount = permiItem.childrens || permiCount;
                return this.findPermi(nextLevels, nextPermiCount);
            }
        }

        return permiCount;
    }

    renderDisplay() {
        let BTNODE = null;
        let {
            isAllow = false, type = 'normal', path = '', children = null, match, wrapClass
        } = this.props;
        let deRepet = Tools.delOption(type.split('-'));

        if (deRepet[0] === 'permission') {
            let matchPath = path || match.path;
            let levels = matchPath.split(',');
            let btnPremis = deRepet.slice(2);
            let usefulPermiList = this.findPermi(levels, btnPremis);

            usefulPermiList.map(item => {
                if (item.path === levels[levels.length - 1]) {
                    let permissions = item.permissions;
                    isAllow = this.judgePermi(btnPremis, permissions);
                    return null;
                }
                return null;
            });
        } else {
            isAllow = true;
        }


        if (!isAllow && (deRepet[1] === 'hidden')) {
            BTNODE = null;
        } else if (!isAllow && (deRepet[1] === 'disabled')) {
            BTNODE = (
                <div className={`cp ${wrapClass} disabled`}> {children} </div>
            )
        } else {
            BTNODE = (
                <div className={`cp ${wrapClass}`}
                     onClick={(e) => this.handleClick(e, isAllow)}> {children} </div>
            )
        }
        return BTNODE;
    };

    handleClick(e, isAllow = false) {
        let disabled = e.target.className.indexOf('disabled') > -1;
        if (disabled)
            return false;

        if (!isAllow) {
            let options = {
                level: 'warning',
                content: `${this.props.warning || '您没有权限操作'}`
            };
            Tools.tooltip(options, null, this.TipClose.bind(this));
            return;
        }

        this.props.clickEvent();

    }

    TipClose() {
        console.log('提示框消失的回掉');
    }

    render() {
        let {permiList = null, type} = this.props;
        let isRun = false;
        if (!type || type.indexOf('normal')) {
            isRun = true;
        } else {
            isRun = !!permiList;
        }
        return !isRun ? null : this.renderDisplay();
    }
}

PerimBtn = connect(state => ({
    permiList: state.userReducer.permi_list || null
}))(PerimBtn);

export default withRouter(PerimBtn);
